package problem236;

//236.二叉树的最近公共祖先
//https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/

//解法一

class Solution1 {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) return null;
        if(root == p || root == q) return root;
        TreeNode ret1 = lowestCommonAncestor(root.left, p, q);
        TreeNode ret2 = lowestCommonAncestor(root.right, p, q);
        if(ret1 != null && ret2 != null) {
            return root;
        }else if(ret1 != null) {
            return ret1;
        }else {
            return ret2;
        }
    }
}